/****************************************************************************
Copyright (c) 2011-2013,WebJet Business Division,CYOU

http://www.genesis-3d.com.cn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using System;
using ScriptRuntime;
using Control;
namespace ScriptRuntime
{

    public class FreeActor : ScriptableClass
    {
        enum MoveState
        {
            None = 0,
            Forward = 1 << 0,
            Backward = 1 << 1,
            Left = 1 << 2,
            Right = 1 << 3,
            ForwardLeft = Forward | Left,
            ForwardRight = Forward | Right,
            BackwardLeft = Backward | Left,
            BackwardRight = Backward | Right,
        }
        protected float mSpeed = 100f;
        protected float mRotationSpeed = 10.0f;
        protected Matrix44 mWorld;
        protected Matrix44 mTempRolation;
        protected Vector2 mScreenPostion = new Vector2(0.0f, 0.0f);
        protected float mMouseY = 0.0f;
        protected float mMouseX = 0.0f;
		public override void OnLoad()
        {
			//SetName("CameraControll");
            
            mWorld = Owner.WorldTransform;
		}

		public override void OnBeginFrame()
		{
            bool move = moveControl();
            bool rotate = rotationControl();
            if (move)
            {
                if (rotate)
                {
                    mWorld = Matrix44.Multiply(mWorld, mTempRolation);
                    normalizeRolation();
                }
                Owner.LocalTransform = mWorld;
            }
            else if (rotate)
            {
                mWorld = Matrix44.Multiply(mWorld, mTempRolation);
                Owner.LocalTransform = mWorld;
                normalizeRolation();
            }
		}

		
		public override void OnFrame()
        {
        }

        protected bool moveControl()
        {
            bool ret = false;
            if (Input.KeyPressed(Code.Q))
            {
                MoveControl.Up(ref mWorld, mSpeed * Util.GetDeltaTime());
                ret = true;
            }
            if (Input.KeyPressed(Code.E))
            {
                MoveControl.Down(ref mWorld, mSpeed * Util.GetDeltaTime());
                ret = true;
            }
            if (Input.MouseWheelBack())
            {
                MoveControl.Backward(ref mWorld, mSpeed * Util.GetDeltaTime());
                ret = true;
            }
            else if (Input.MouseWheelForward())
            {
                MoveControl.Forward(ref mWorld, mSpeed * Util.GetDeltaTime());
                ret = true;
            }

            MoveState state = MoveState.None;
            if (Input.KeyPressed(Code.W))
            {
                state |= MoveState.Forward;
            }
            else if (Input.KeyPressed(Code.S))
            {
                state |= MoveState.Backward;
            }
            if (Input.KeyPressed(Code.A))
            {
                state |= MoveState.Left;
            }
            else if (Input.KeyPressed(Code.D))
            {
                state |= MoveState.Right;
            }


            switch (state)
            {
                case MoveState.Forward:
                    {
                        MoveControl.Forward(ref mWorld, mSpeed * Util.GetDeltaTime());
                        ret = true;
                    }
                    break;
                case MoveState.Backward:
                    {
                        MoveControl.Backward(ref mWorld, mSpeed * Util.GetDeltaTime());
                        ret = true;
                    }
                    break;
                case MoveState.Left:
                    {
                        MoveControl.Left(ref mWorld, mSpeed * Util.GetDeltaTime());
                        ret = true;
                    }
                    break;
                case MoveState.Right:
                    {
                        MoveControl.Right(ref mWorld, mSpeed * Util.GetDeltaTime());
                        ret = true;
                    }
                    break;
                case MoveState.ForwardLeft:
                    {
                        MoveControl.ForwardLeft(ref mWorld, mSpeed * Util.GetDeltaTime());
                        ret = true;
                    }
                    break;
                case MoveState.ForwardRight:
                    {
                        MoveControl.ForwardRight(ref mWorld, mSpeed * Util.GetDeltaTime());
                        ret = true;
                    }
                    break;
                case MoveState.BackwardLeft:
                    {
                        MoveControl.BackwardLeft(ref mWorld, mSpeed * Util.GetDeltaTime());
                        ret = true;
                    }
                    break;
                case MoveState.BackwardRight:
                    {
                        MoveControl.BackwardRight(ref mWorld, mSpeed * Util.GetDeltaTime());
                        ret = true;
                    }
                    break;
                default:
                    {
                        ret |= false;
                    }
                    break;
            }
            return ret;
        }

        protected bool rotationControl()
        {
            if (Input.MouseButtonDown(MouseCode.RightButton))
            {
                Input.GetScreenPosition(out mScreenPostion);
            }
            else if (Input.MouseButtonPressed(MouseCode.RightButton))
            {
                Vector2 temp;
                Input.GetScreenPosition(out temp);
                Vector2 bias = temp - mScreenPostion;
                mScreenPostion = temp;
                if (bias.X != 0.0f || bias.Y != 0.0f)
                {
                    Matrix44 right = Matrix44.CreateRotationX(-bias.Y * mRotationSpeed);
                    Matrix44 up = Matrix44.CreateRotationY(-bias.X * mRotationSpeed);
                    Matrix44.Multiply(ref right, ref up, out mTempRolation);
                    return true;
                }
            }
            return false;
        }

        protected void normalizeRolation()
        {
            Vector3 right = mWorld.Right;
            right.Y = 0.0f;
            right.Normalize();

            Vector3 back = Vector3.Cross(right, mWorld.Up);
            Vector3 up = Vector3.Cross(back, right);
            mWorld.Right = right;
            mWorld.Backward = back;
            mWorld.Up = up;
        }
    }
}